#!/usr/bin/make -f
# -*- makefile -*-

# Predesys - Server
# Author: Jose A. Jimenez <jajimc@gmail.com>
# License: GPL 3.0 (see "license" file)

NAME 				= predesys-server
WEBSERVNAME			= predesys-server-webservice

DEST_BINDIR			= /usr/bin
DEST_INITDDIR		= /etc/init.d
DEST_ETCDIR			= /etc/$(NAME)

DEST_MAINDIR 		= /usr/share/$(NAME)
DEST_IUDIR			= $(DEST_MAINDIR)/adminui
DEST_COREDIR		= $(DEST_MAINDIR)/core
DEST_DATADIR		= $(DEST_MAINDIR)/data
DEST_SERVDIR		= $(DEST_MAINDIR)/services
DEST_WEBSERVDIR		= $(DEST_MAINDIR)/webservice
DEST_ERRORLOGDIR 	= $(DEST_MAINDIR)/errorlog

SRC_MAINDIR			= src
SRC_IUDIR			= $(SRC_MAINDIR)/adminui
SRC_COREDIR			= $(SRC_MAINDIR)/core
SRC_DATADIR			= $(SRC_MAINDIR)/data
SRC_SERVDIR			= $(SRC_MAINDIR)/services
SRC_WEBSERVDIR		= $(SRC_MAINDIR)/webservice
SRC_ERRORLOGDIR		= $(SRC_MAINDIR)/errorlog

SRC_CONFFILE		= $(SRC_COREDIR)/configuration.xml
SRC_INITDFILE		= $(SRC_WEBSERVDIR)/$(WEBSERVNAME)
SRC_ABOUTFILE		= about
SRC_LICENSEFILE		= license

compile:

install:	basic-install

basic-install:
	mkdir -p $(DEST_BINDIR) $(DEST_INITDDIR) $(DEST_ETCDIR)
	mkdir -p $(DEST_MAINDIR) $(DEST_IUDIR) $(DEST_COREDIR) $(DEST_DATADIR) $(DEST_SERVDIR) $(DEST_WEBSERVDIR) $(DEST_ERRORLOGDIR)
	
	install -p -m 0644 $(SRC_IUDIR)/*.py $(DEST_IUDIR)
	chmod 0755 $(DEST_IUDIR)/predesysserver.py
	install -p -m 0644 $(SRC_COREDIR)/*.py $(DEST_COREDIR)
	install -p -m 0644 $(SRC_DATADIR)/*.sql $(DEST_DATADIR)
	install -p -m 0644 $(SRC_SERVDIR)/*.py $(DEST_SERVDIR)
	install -p -m 0644 $(SRC_ERRORLOGDIR)/*.py $(DEST_ERRORLOGDIR)
	
	install -p -m 0644 $(SRC_CONFFILE) $(DEST_ETCDIR)
	
	install -p -m 0644 $(SRC_WEBSERVDIR)/daemon.py $(DEST_WEBSERVDIR)
	install -p -m 0744 $(SRC_WEBSERVDIR)/predesysserverwebservice.py $(DEST_WEBSERVDIR)
		
	install -p -m 0644 $(SRC_ABOUTFILE) $(DEST_MAINDIR)
	install -p -m 0644 $(SRC_LICENSEFILE) $(DEST_MAINDIR)
	
	ln -s -f $(DEST_IUDIR)/predesysserver.py $(DEST_BINDIR)/predesys-server
	
	# Web service installation
	install -p -m 0744 $(SRC_INITDFILE) $(DEST_INITDDIR)
	update-rc.d $(WEBSERVNAME) defaults
	- service $(WEBSERVNAME) start

uninstall:
	# Web service uninstallation
	- service $(WEBSERVNAME) stop
	rm -f $(DEST_INITDDIR)/$(WEBSERVNAME)
	update-rc.d $(WEBSERVNAME) remove

	rm -f $(DEST_BINDIR)/predesys-server
	rm -rf $(DEST_ETCDIR)
	rm -rf $(DEST_MAINDIR)
